package sfeir.ergosum.menu;

import sfeir.ergosum.AboutActivity;
import sfeir.ergosum.AddContactActivity;
import sfeir.ergosum.ContactManagerActivity;
import sfeir.ergosum.HiveActivity;
import sfeir.ergosum.MyProfileActivity;
import sfeir.ergosum.PreferencesActivity;
import sfeir.ergosum.R;
import sfeir.ergosum.myprofile.SelectProfileActivity;
import sfeir.ergosum.service.ContactService;
import sfeir.ergosum.tools.ThemeColor;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.sfeir.proto.hivelayout.HiveAction;
import com.sfeir.proto.hivelayout.HiveActionCell;
import com.sfeir.proto.hivelayout.HiveMenuView;

public class HiveMainMenuActivity extends Activity {

    private HiveMenuView hmv;

    ThemeColor theme ;
    private int colorPistil;
    private int colorPetal;

    private Thread mThread;
    private Handler mHandler = new Handler();

    final Runnable setupHiveMenu = new Runnable() {
        public void run() {
            hmv.setup();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        
        theme = new ThemeColor(HiveMainMenuActivity.this);
        colorPetal = theme.getMenuColors().get(0);
        colorPistil = theme.getMenuColors().get(1);

        hmv = new HiveMenuView(this);
        //hmv.setBackgroundColor(0x40000000);
//        hmv.setBackgroundResource(R.drawable.bg);

        // TODO: needed if thread should be removed
        //hmv.setConstants(width, height);
 
        BitmapDrawable wallPaper = HiveActivity.getWallDrawable();
        if(wallPaper!=null)
            hmv.setBackgroundDrawable(wallPaper);

        final HiveAction action1 = new HiveAction(getString(R.string.main_menu_my)) {
            public void executeAction() {
                SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(HiveMainMenuActivity.this);
                String key = prefs.getString("profile_spreadsheet_key", null);
                if (null == key) {
                    HiveMainMenuActivity.this.startActivity(new Intent(HiveMainMenuActivity.this, SelectProfileActivity.class));
                } else {
                    HiveMainMenuActivity.startProfileDetailsActivity(HiveMainMenuActivity.this, key);
                }
            };
        };

        final HiveAction action2 = new HiveAction(getString(R.string.main_menu_add)) {
            public void executeAction() {
                HiveMainMenuActivity.this.startActivity(new Intent(HiveMainMenuActivity.this, AddContactActivity.class));
            };
        };

        final HiveAction action3 = new HiveAction(getString(R.string.add_group)) {
            public void executeAction() {
                HiveMainMenuActivity.addGroup(HiveMainMenuActivity.this);
            };
        };

        final HiveAction action4 = new HiveAction(getString(R.string.main_menu_update)) {
            public void executeAction() {
                ContactService.actionUpdateContact(HiveMainMenuActivity.this);
                Toast.makeText(HiveMainMenuActivity.this, R.string.update_start, 500).show();
            };
        };

        final HiveAction action5 = new HiveAction(getString(R.string.main_menu_preference)) {
            public void executeAction() {
                HiveMainMenuActivity.this.startActivity(new Intent(HiveMainMenuActivity.this, PreferencesActivity.class));
            };
        };

        final HiveAction action6 = new HiveAction(getString(R.string.main_menu_about)) {
            public void executeAction() {
                HiveMainMenuActivity.this.startActivity(new Intent(HiveMainMenuActivity.this, AboutActivity.class));
            };
        };


        mThread = new Thread() {

            public void run() {
                Looper.prepare();

                HiveActionCell c1 =
                    new HiveActionCell(HiveMainMenuActivity.this, action1, R.drawable.identity);
                c1.setShown(true);
                c1.setCellColor(colorPetal);
                c1.setEmboss(true);
                hmv.setHiveMenuCell1(c1);

                HiveActionCell c2 =
                    new HiveActionCell(HiveMainMenuActivity.this, action2, R.drawable.add_user);
                c2.setShown(true);
                c2.setCellColor(colorPetal);
                c2.setEmboss(true);
                hmv.setHiveMenuCell2(c2);

                HiveActionCell c3 =
                    new HiveActionCell(HiveMainMenuActivity.this, action3, R.drawable.add_group);
                c3.setShown(true);
                c3.setCellColor(colorPetal);
                c3.setEmboss(true);
                hmv.setHiveMenuCell3(c3);

                HiveActionCell c4 =
                    new HiveActionCell(HiveMainMenuActivity.this, action4, R.drawable.refresh);
                c4.setShown(true);
                c4.setCellColor(colorPetal);
                c4.setEmboss(true);
                hmv.setHiveMenuCell4(c4);

                HiveActionCell c5 =
                    new HiveActionCell(HiveMainMenuActivity.this, action5, R.drawable.utilities);
                c5.setShown(true);
                c5.setCellColor(colorPetal);
                c5.setEmboss(true);
                hmv.setHiveMenuCell5(c5);

                HiveActionCell c6 =
                    new HiveActionCell(HiveMainMenuActivity.this, action6, R.drawable.about);
                c6.setShown(true);
                c6.setCellColor(colorPetal);
                c6.setEmboss(true);
                hmv.setHiveMenuCell6(c6);

                HiveAction actionCancel = new HiveAction("MENU") {
                    public void executeAction() {
                        finish();
                    };
                };
                HiveActionCell cs = new HiveActionCell(HiveMainMenuActivity.this, actionCancel);
                cs.setShown(true);
                cs.setCellColor(colorPistil);
                cs.setEmboss(true);
                hmv.setHiveStatusMenuCell(cs);

                do {

                } while (!hmv.isConstantsOk());
                mHandler.post(setupHiveMenu);
                Looper.loop();

            }
        };
        mThread.start();

        setContentView(hmv);
    }

    public static void startProfileDetailsActivity(Activity activity, String key) {
        Intent intent = new Intent(activity, MyProfileActivity.class);
        intent.putExtra("key", key);
        activity.startActivityForResult(intent, HiveActivity.REFRESH);
    }

    private static void addGroup(final Activity activity) {
        LayoutInflater factory = LayoutInflater.from(activity);
        final View textEntryView = 
            factory.inflate(R.layout.input_text_dialog, null);
        final TextView textInstruction = 
            (TextView) textEntryView.findViewById(R.id.instruction_dialog);
        final EditText text = 
            (EditText) textEntryView.findViewById(R.id.input_dialog);

        textInstruction.setText(R.string.add_group_instruction);

        AlertDialog dialog =  new AlertDialog.Builder(activity)
        .setTitle(R.string.add_group)
        .setView(textEntryView)
        .setPositiveButton(R.string.dialog_ok, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                /* User clicked OK so do some stuff */
                String nameGroup;
                if (text.getText().length() == 0)
                    nameGroup = activity.getString(R.string.add_default_name_group);
                else
                    nameGroup = text.getText().toString().trim();

                ContactManagerActivity.addGroup(activity, nameGroup);
                HiveActivity.startActivity(activity, "", "");
            }
        }).setNegativeButton(R.string.dialog_cancel, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                /* User clicked cancel so do some stuff */
            }
        }).create();
        dialog.show();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        this.finish();
        return true;
    }
}

